package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.items.entity.Zykjs;
import com.gfxy.modules.items.entity.teachingresult.TeachingResultContent;
import com.gfxy.modules.items.mapper.ZykjsMapper;
import com.gfxy.modules.items.service.IZykjsService;
import com.gfxy.modules.items.vo.ZykisMenberPage;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 资源库建设
 * @Author: jeecg-boot
 * @Date: 2021-11-01
 * @Version: V1.0
 */
@Service
public class ZykjsServiceImpl extends AbstractProjectServiceImpl<ZykjsMapper, Zykjs> implements IZykjsService {
    protected int memberCellNum = 5;


    @Override
    protected void pass(String id, String isPass) {

    }

    @Override
    protected Zykjs getOneProject(Row row) {
        String year = ExcelUtil.formatCell(row.getCell(0));
        String classessStr = ExcelUtil.formatCell(row.getCell(1));
        String award = ExcelUtil.formatCell(row.getCell(2));
        String step = ExcelUtil.formatCell(row.getCell(3));
        String projectName = ExcelUtil.formatCell(row.getCell(4));
        Zykjs jxcg = new Zykjs();
        jxcg.setDate(year);
        jxcg.setClasses(this.translateClassess(classessStr));
        jxcg.setStep(this.step(step));
        jxcg.setAward(this.translateAward(award));
        jxcg.setProjectName(projectName);
        jxcg.setIsPass(0);

        //这个地方不要忘了-算总分
        jxcg.setTotalScore(this.getTotalScore(jxcg));
        return jxcg;
    }

    private Integer translateAward(String award) {
        if (award == null || award.equals("")) {
            return 0;
        } else if (award.equals("国家级")) {
            return 1;
        } else if (award.equals("省级")) {
            return 2;
        }
        return 0;
    }


    private Integer translateClassess(String classessStr) {
        if (classessStr == null || classessStr.equals("")) {
            return 0;
        } else if (classessStr.equals("主持")) {
            return 1;
        } else if (classessStr.equals("联合主持")) {
            return 2;
        } else if (classessStr.equals("子项目")) {
            return 3;
        } else {
            return 0;
        }
    }

    public Integer step(String name) {
        if (name == null || name.equals("")) {
            return 0;
        } else if (name.equals("立项")) {
            return 1;
        } else if (name.equals("验收")) {
            return 2;
        }
        return 0;
    }

    @Override
    public Double getTotalScore(Zykjs zykjs) {
        double result = 0.0;
        int classess = zykjs.getClasses();
        int award = zykjs.getAward();
        Integer step = zykjs.getStep();
        if (award == 1) {
            if (classess == 1) {
                result = 400;
            } else if (classess == 2) {
                result = 200;
            } else if (classess == 3) {
                result = 40;
            }
        } else if (award == 2) {
            if (classess == 1) {
                result = 200;
            } else {
                result = 0.0;
            }
        }
        if (step == 1) {
            result = result * 0.3;
        } else if (step == 2) {
            result = result * 0.7;
        }
        return result;
    }

    @Override
    public Page<ZykisMenberPage> getMemberPageListByYear(String date, Integer pageNo, Integer pageSize) {
        Page<ZykisMenberPage> zxjpkMemberPagePage = new Page<>(pageNo, pageSize);
        List<ZykisMenberPage> zykisMenberPages = this.baseMapper.selectListByYear(date);
        System.out.println(zykisMenberPages + "ggggggggggggggg");
        return zxjpkMemberPagePage.setRecords(zykisMenberPages);


    }

    @Override
    public List<ZykisMenberPage> getMemberPageListByYear(String date) {
        return this.baseMapper.selectListByYear(date);
    }

    @Override
    protected String[] getMemberArray(Row row) {
        String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
        return nameStrs.split(memberNameSeparator);
    }

    @Override
    protected List<TeachingResultContent> createTeachingResultContent(Zykjs jxcg) {

        List<TeachingResultContent> result = new ArrayList<>();

        // 类型
        TeachingResultContent classTeachingResultContent = new TeachingResultContent();
        classTeachingResultContent.setType(1);
        classTeachingResultContent.setLevel(jxcg.getClasses());

        try {
            classTeachingResultContent.setValue(dictTranslater.getDictValue(ZykisMenberPage.class, "classes", String.valueOf(jxcg.getClasses())));
            result.add(classTeachingResultContent);
        } catch (NoSuchFieldException e) {
            log.error(e.getMessage(), e);
        }

        // 类型
        TeachingResultContent awTeachingResultContent = new TeachingResultContent();
        awTeachingResultContent.setType(1);
        awTeachingResultContent.setLevel(jxcg.getAward());

        try {
            classTeachingResultContent.setValue(dictTranslater.getDictValue(ZykisMenberPage.class, "award", String.valueOf(jxcg.getAward())));
            result.add(classTeachingResultContent);
        } catch (NoSuchFieldException e) {
            log.error(e.getMessage(), e);
        }

        TeachingResultContent stepTeachingResultContent = new TeachingResultContent();
        stepTeachingResultContent.setType(1);
        stepTeachingResultContent.setLevel(jxcg.getStep());

        try {
            stepTeachingResultContent.setValue(dictTranslater.getDictValue(ZykisMenberPage.class, "step", String.valueOf(jxcg.getStep())));
            result.add(stepTeachingResultContent);
        } catch (NoSuchFieldException e) {
            log.error(e.getMessage(), e);
        }

        return result;
    }
}
